package com.yuyou.tas.admin.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yuyou.tas.util.bean.LoginRedisObj;



public class BaseController {
	
	@Autowired
	private StringRedisTemplate stringRedisTemplate;

	
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }
	
    protected LoginRedisObj getUserObj(HttpServletRequest request) {
        String accessToken = request.getHeader("accessToken");
        if(StringUtils.isEmpty(accessToken)){
        	accessToken = request.getParameter("accessToken") ;
        }
        String jsonObj=stringRedisTemplate.opsForValue().get(accessToken);
        LoginRedisObj obj=JSONObject.parseObject(jsonObj, new TypeReference<LoginRedisObj>() {});
        
        return obj;
    }
}
